package com.chzu.controller;

import com.chzu.pojo.ScoreInfo;
import com.chzu.pojo.SysUser;
import com.chzu.result.JsonResult;
import com.chzu.result.QueryInfo;
import com.chzu.service.ScoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName ScoreInfoController
 * @Description TODO
 * @Date 2022/02/22 21:42
 */
@RestController
@RequestMapping("/scoreInfo")
@Api(tags = "考试成绩的接口")
public class ScoreInfoController {

    @Resource
    private ScoreInfoService scoreInfoService;

    @ApiOperation("获得学生的成绩信息")
    @PostMapping("/")
    public JsonResult<Object> getScoreInfo(@RequestBody QueryInfo queryInfo) {
        return scoreInfoService.getScoreInfo(queryInfo);
    }

    @ApiOperation("添加学生成绩信息")
    @Transactional(rollbackFor= Exception.class)
    @PostMapping("/addScoreInfo")
    public JsonResult<Object> addScoreInfo(@RequestBody ScoreInfo scoreInfo) {
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = user.getUsername();
        scoreInfo.setStudentId(userId);
        scoreInfo.setEndTime(new Date());
        scoreInfo.setIsTested(true);
        scoreInfo.setStatus(true);
        boolean save = scoreInfoService.save(scoreInfo);
        if (!save) {
            return JsonResult.fail("添加学生信息失败");
        }
        return JsonResult.ok("添加学生信息成功");
    }

    @ApiOperation("获得所有学生的所有次考试成绩信息")
    @PostMapping("/getAllStudentExamInfo")
    public JsonResult<Object> getAllStudentExamInfo(@RequestBody QueryInfo queryInfo) {
        return scoreInfoService.getAllStudentExamInfo(queryInfo);
    }

    @ApiOperation("获得最新一次考试学生的成绩")
    @GetMapping("/getLastExamScore")
    public JsonResult<Object> getLastExamScore() {
        List<Integer> data = scoreInfoService.getLastExamScore();
        return JsonResult.ok(data);
    }
    @ApiOperation("判断当前用户是否已经进行了测试")
    @PostMapping("/hasTesting/{paperId}")
    public JsonResult<Object> hasTesting(@PathVariable("paperId") String paperId) {
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = user.getUsername();
        int res = scoreInfoService.hasTesting(userId, paperId);
        return JsonResult.ok(res);
    }


}
